<?php include('header.php');?>
<?php
//$roomtype_id = $_GET['id'];

if($_GET['action']=='edit') {

	if($_POST["room_name"]!="" && $_POST["roomtype_id"]!= ""){

		$Query = mysql_query("SELECT room_name FROM room WHERE room_name = '".$_POST["room_name"]."' AND roomtype_id = '".$_POST["roomtype_id"]."' ");
		$Num = mysql_num_rows($Query);
		if($Num>='1'){
			$save = "ชื่อห้อง / ราคาห้อง / ประเภทห้องพัก  มีอยู่แล้วค่ะ";
			$room_name = $_POST['room_name'];
			$roomtype_id = $_POST["roomtype_id"];
			$room_id = $_POST["room_id"];
		} else {
			$QueryAdd = mysql_query("UPDATE room SET room_name = '".$_POST["room_name"]."',roomtype_id = '".$_POST["roomtype_id"]."'  WHERE room_id = '".$_POST['room_id']."' ");
			if($QueryAdd){
				$save = "<script>window.location='room_manage.php'</script>";
			}
		}
	}
}

if($_GET['id']!=""){
	$Query = mysql_query("SELECT * FROM room WHERE room_id = '".$_GET["id"]."' ");
	$Row = mysql_fetch_array($Query);
		$room_name = $Row['room_name'];
		$roomtype_id = $Row["roomtype_id"];
		$room_id = $_GET['id'];
}
?>
<body>
<form action="?action=edit" name="formedit" method="post" id="formedit">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>แก้ไขประเภทห้องพัก</h1>
      <div class="buttons"><a onclick="chk_form('formedit');" class="button"><span>บันทึก</span></a><a onclick="window.location = 'room_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form">
       <tr>
        <td>ชื่อห้อง : </td>
        <td><input type="hidden" name="room_id" value="<?php echo $room_id;?>" ><input name="room_name" type="text" id="room_name" value="<?php echo $room_name;?>" maxlength="100" onkeyup="IsScharacter(this.value,this)" /></td>
      </tr>
       <tr>
        <td>ประเภทห้องพัก : </td>
        <td>
        <select name="roomtype_id">
        	<option value=""><--- กรุณาเลือกประเภทห้องพัก ---></option>
<?php 
	$rQuery = mysql_query("SELECT * FROM roomtype ORDER BY roomtype_name");
	while($rRow = mysql_fetch_array($rQuery)){
?>
			<option <?php if($roomtype_id==$rRow['roomtype_id']){echo "Selected";}?> value="<?php echo $rRow['roomtype_id'];?>"><?php echo $rRow['roomtype_name'];?></option>
<?php } ?>
        </select>
        </td>
      </tr>   
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</form>
<?php mysql_close(); ?>
</body>
</html>
